package com.fjwt.gz.core.model.security;

import com.fjwt.gz.core.entity.SysUser;
import com.fjwt.gz.core.jwt.JWTPayload;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;

/*
* 实现Spring Security的UserDetails接口
*
* @author wg
* @site https://pt.jdyd.xyz/
* @date 2021/6/8 16:34
*/
@Slf4j
@Data
public class GzUserDetails implements UserDetails {

    /** 系统用户信息 **/
    private SysUser sysUser;

    /** 密码 **/
    private String credential;

    /** 角色+权限 集合   （角色必须以： ROLE_ 开头） **/
    private Collection<SimpleGrantedAuthority> authorities = new ArrayList<>();

    /** 缓存标志 **/
    private String cacheKey;

    /** 登录类型 */
    private String loginType;

    /** 登录IP **/
    private String loginIp;

    /** 认证类型，见  CREDENTIAL_AUTH_TYPE **/
    private String authType;

    /** 小程序fansId **/
    private Long fansId;

    /** 小程序userId **/
    private Long userId;

    /** 小程序appId **/
    private String appId;

    /** 小程序agentNo **/
    private String agentNo;

    /** 小程序用户商户号 **/
    private String mchNo;

    //此处的无参构造，为json反序列化提供
    public GzUserDetails() {
    }

    public GzUserDetails(SysUser sysUser, String credential) {

        this.setSysUser(sysUser);
        this.setCredential(credential);

        //做一些初始化操作
    }

    public GzUserDetails(SysUser sysUser,Long fansId, Long userId) {
        this.setSysUser(sysUser);
        this.setFansId(fansId);
        this.setUserId(userId);
        //做一些初始化操作
    }
    /** spring-security 需要验证的密码 **/
    @Override
    public String getPassword() {
        return getCredential();
    }

    /** spring-security 登录名 **/
    @Override
    public String getUsername() {
        return getSysUser().getSysUserId() + "";
    }

    /** 账户是否过期 **/
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    /** 账户是否已解锁 **/
    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    /** 密码是否过期 **/
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    /** 账户是否开启 **/
    @Override
    public boolean isEnabled() {
        return true;
    }

    /** 获取权限集合 **/
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return authorities;
    }

    public static GzUserDetails getCurrentUserDetails() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication == null) {
            return null;
        }

        try {
            return (GzUserDetails) authentication.getPrincipal();
        }catch (Exception e) {
            return null;
        }
    }


    // 判断是否app登录
    public boolean isAppLogin(){
        return JWTPayload.LOGIN_PAGE_TYPE.APP.equals(getLoginType()) || JWTPayload.LOGIN_PAGE_TYPE.LITE.equals(getLoginType());
    }



    /** 获取当前登录者ID **/
    public Long getSysUserId(){
        return this.getSysUser() != null ? this.getSysUser().getSysUserId() : null;
    }


}
